
#pragma warning(disable : 4819)
#include "BeginScene.h"
#include <iostream>
#include <SDL_image.h>
#include "Game.h"
BeginScene::BeginScene()
{
    status_= TankeStatus::default_status;
    //场景默认为初始画面
    sceneSelect_= SceneSelect::default_scene;
    // 初始化容器映射表
    SDL_eventMap_.insert(std::make_pair(SDLK_w, std::bind(&BeginScene::last_select, this)));
    SDL_eventMap_.insert(std::make_pair(SDLK_s, std::bind(&BeginScene::Next_select, this)));
    SDL_eventMap_.insert(std::make_pair(SDLK_j, std::bind(&BeginScene::sure_select, this)));

    //初始化场景映射表
    SDL_SceneMap_.insert(std::make_pair(SceneSelect::default_scene, std::bind(&BeginScene::Render_default_scene, this)));
    SDL_SceneMap_.insert(std::make_pair(SceneSelect::Team_Scene, std::bind(&BeginScene::Render_Tream_scene, this)));
    SDL_SceneMap_.insert(std::make_pair(SceneSelect::Create_Scene, std::bind(&BeginScene::Render_create_scene, this)));
 
    auto& game = Game::GetInstance();
    //加载图片
    currentTexture_ = IMG_LoadTexture(game.GetRenderer(), "fodder/pic/tank_main.png");
    if(currentTexture_ == nullptr)
    {
        std::cerr << "IMG_LoadTexture Error: " << IMG_GetError() << std::endl;
        return;
    }
    currentTextureTanke_ = IMG_LoadTexture(game.GetRenderer(), "fodder/pic/tank_sprite.png");
    if(currentTexture_ == nullptr)
    {
        std::cerr << "IMG_LoadTexture Error: " << IMG_GetError() << std::endl;
        return;
    }
    //加载音乐
    music = Mix_LoadMUS("fodder/sounds/intro.wav");
    if(music == nullptr)
    {
        std::cerr << "Mix_LoadMUS Error: " << Mix_GetError() << std::endl;
        return;
    }
    //播放音乐
    Mix_PlayMusic(music, -1);
    int time = 3000;
    //设置定时器
    
}

BeginScene::~BeginScene()
{
    SDL_DestroyTexture(currentTexture_);
    Mix_FreeMusic(music);
}
void BeginScene::Init()
{
    
}
// 接收事件
void BeginScene::HandleEvent(SDL_Event *event)
{
    if (event->type == SDL_KEYDOWN)
    {
        auto it = SDL_eventMap_.find(event->key.keysym.sym);
        if (it != SDL_eventMap_.end())
        {
            it->second();
        }
    }
}
//更新画面
UpDateResult BeginScene::Update(float deltaTime)
{   
   
    static float time=0;
    time+=deltaTime;
    if(time>=100)
    {
        bool flag=static_cast<int>(status_);
        flag=!flag;
       status_=static_cast<TankeStatus>(flag);
       time=0;
    }
       return  updateResult_;
}
//根据映射表不同的信息设置不同画面
void BeginScene::Render()
{
    auto it = SDL_SceneMap_.find(sceneSelect_);
    if (it != SDL_SceneMap_.end())
    {
        it->second();
    }
}
void BeginScene::exit_()
{

}
// 选项下移
void BeginScene::Next_select()
{
    //std::cout<<"sceneSelect_"<<sceneSelect_<<std::endl;
   status_=TankeStatus::run_status;
   int now=static_cast<int>(sceneSelect_);
   if(++now==static_cast<int>(SceneSelect::max_scene))
   {
       sceneSelect_=SceneSelect::default_scene;
   }
   else
   {
       sceneSelect_=static_cast<SceneSelect>(now);
   }

}
// 选项上移
void BeginScene::last_select()
{
    status_ = TankeStatus::run_status;
    int now = static_cast<int>(sceneSelect_);
    if (now-- == 0)
    {
        sceneSelect_ = SceneSelect::Create_Scene;
    }
    else
    {
        sceneSelect_ = static_cast<SceneSelect>(now);
    }
}
// 设置选项
void BeginScene::sure_select()
{
    updateResult_.isSwitchScene = true;
    if (sceneSelect_ == SceneSelect::default_scene || sceneSelect_ == SceneSelect::Team_Scene)
    {
        updateResult_.sceneType = SceneType::SceneGame;
    }
    if (sceneSelect_ == SceneSelect::Create_Scene)
    {
        std::cout << "create_scene" << std::endl;
        updateResult_.sceneType = SceneType::SceneMakeMap;
    }
}       
void BeginScene::Render_default_scene()
{
    auto renderer = Game::GetInstance().GetRenderer();
    // 渲染
    // 清屏
    SDL_RenderClear(renderer);
    // 画
    SDL_Rect pic_rect = {0, 0, 780, 780}; // 图片显示区域
    SDL_Rect pic_rect_tanke;
    if (TankeStatus::run_status == status_)
    {
        // std::cout<<"run_status"<<std::endl;
        pic_rect_tanke = {68, 0, 34, 34}; // 图片显示区域
    }
    else
    {
        // std::cout<<"run_status1"<<std::endl;
        pic_rect_tanke = {102, 0, 34, 34}; // 图片显示区域
    }
    SDL_RenderCopy(renderer, currentTexture_, nullptr, &pic_rect);
    pic_rect = {250, 405, 40, 40}; // 图片显示区域
    SDL_RenderCopy(renderer, currentTextureTanke_, &pic_rect_tanke, &pic_rect);
    // 更新显示
    SDL_RenderPresent(renderer);

    SDL_RenderClear(renderer);
}

void BeginScene::Render_Tream_scene()
{
    auto renderer = Game::GetInstance().GetRenderer();
    // 渲染
    // 清屏
    SDL_RenderClear(renderer);
    // 画图片
    SDL_Rect pic_rect = {0, 0,780,780}; // 图片显示区域
    SDL_Rect pic_rect_tanke;
    if(TankeStatus::run_status==status_)
    {
       // std::cout<<"run_status"<<std::endl;
        pic_rect_tanke = {68,0,34,34}; // 图片显示区域
    }
    else
    {
        //std::cout<<"run_status1"<<std::endl;
        pic_rect_tanke = {102,0,34,34}; // 图片显示区域
    }
    SDL_RenderCopy(renderer, currentTexture_, nullptr, &pic_rect);
    pic_rect = {250,465,40,40}; // 图片显示区域
    SDL_RenderCopy(renderer, currentTextureTanke_, &pic_rect_tanke, &pic_rect);
    // 更新显示
    SDL_RenderPresent(renderer);

    SDL_RenderClear(renderer);
}
void BeginScene::Render_create_scene()
{
  
    auto renderer = Game::GetInstance().GetRenderer();
    // 渲染
    // 清屏
    SDL_RenderClear(renderer);
    // 画图片
    SDL_Rect pic_rect = {0, 0,780,780}; // 图片显示区域
    SDL_Rect pic_rect_tanke;
    if(TankeStatus::run_status==status_)
    {
       // std::cout<<"run_status"<<std::endl;
        pic_rect_tanke = {68,0,34,34}; // 图片显示区域
    }
    else
    {
        //std::cout<<"run_status1"<<std::endl;
        pic_rect_tanke = {102,0,34,34}; // 图片显示区域
    }
    SDL_RenderCopy(renderer, currentTexture_, nullptr, &pic_rect);
    pic_rect = {250,525,40,40}; // 图片显示区域
    SDL_RenderCopy(renderer, currentTextureTanke_, &pic_rect_tanke, &pic_rect);
    // 更新显示
    SDL_RenderPresent(renderer);
}
